

    const passport = require('koa-passport')
    var localStrategy = require('passport-local').Strategy
    const jwtStrategy = require('passport-jwt').Strategy
    const ExtractJwt = require('passport-jwt').ExtractJwt
    const filesystemService = require('../service/filesystemService')
    const baseConfig = require('../config/base-config')

    const opts = {}
    opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken()
    opts.secretOrKey = baseConfig.jwt.serectKey
    passport.use(new jwtStrategy(
        opts,
        async (jwt_payloads, done) => {
            console.log(jwt_payloads)
            console.log("发布时间" + new Date(jwt_payloads.iat))
            console.log("过期时间" + new Date(jwt_payloads.exp))
            let nowDate = new Date()
            if(nowDate.getTime() <= jwt_payloads.exp) {
                const user = await filesystemService.checkUser(username, password)
                if(user) {
                    return done(null, user, 'token验证成功')
                }
                else {
                    return done(null, false, 'token验证失败')
                }
            }
            else {
                done(null, false, 'token过期')
            }
        }
    ))

    passport.use(new localStrategy(
        {
            usernameField: 'username',
            passwordField: 'password'
        },
        async function(username, password, done) {
            let result = await filesystemService.checkUser(username, password)
            console.log(result)
            if(result) {
                return done(null, result, '登陆成功')
            }
            else {
                return done(null, false, '账号密码错误')
            }
        }
    ))

    passport.serializeUser(function(user, done) {
        user.password = ''
        user.salt = ''
        console.log('user_ps:' + user)
        done(null, user)
    })

    passport.deserializeUser(function(user, done) {
        done(null, user)
    })

    module.exports = passport